﻿using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SmartHotel.Dto;
using SmartHotel.Menu;
using SmartHotel.Role.Dto;
using SmartHotel.Roles;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace SmartHotel.Role
{
    [ApiExplorerSettings(GroupName = "v2")]
    public class SmartHotelAppRoleService : ApplicationService, ISmartHotelAppRoleService//Used to create/update a book]
    {
        public SmartHotelAppRoleService(IRepository<RoleTable, int> repository) 
        {
            Role = repository;
        }

        public IRepository<RoleTable, int> Role { get; }
        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<ResultDto<CreateandUpdateRole>> CreateRoleAsync(CreateandUpdateRole input)
        {
            var role = ObjectMapper.Map<CreateandUpdateRole, RoleTable>(input);
            role = await Role.InsertAsync(role);

            var dto = ObjectMapper.Map<RoleTable, CreateandUpdateRole>(role);
            return new ResultDto<CreateandUpdateRole> { Code = Result.Success, Data = dto }; 
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public  async Task<ResultDto<bool>> DeleteRoleAsync(int id)
        {
            await Role.DeleteAsync(id);
            return new ResultDto<bool> { Code= Result.Success };
            
        }
        /// <summary>
        /// 反填角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ResultDto<RoleDto>> GetFindRoleAsync(int id)
        {
            var list = await Role.GetAsync(id);
            return new ResultDto<RoleDto> { Code = Result.Success, Data = list };
        }
        /// <summary>
        /// 查询角色
        /// </summary>
        /// <returns></returns>
        public async Task<ResultDto<List<RoleDto>>> GetRoleAsync()
        {
            var roles = await Role.GetListAsync();

            var dto= ObjectMapper.Map<List<RoleTable>, List<RoleDto>>(roles);
            
            return new ResultDto<List<RoleDto>>{ Code = Result.Success, Data = dto };
        }

        /// <summary>
        /// 角色分页
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="RoleName"></param>
        /// <returns></returns>
        public async Task<PageUserRoleDto<List<RoleDto>>> GetPageListRole(int pageIndex, int pageSize,  string RoleName)

       

        {
            var totalcount = await Role.GetCountAsync();
            var list = await Role.GetQueryableAsync();
            if (!string.IsNullOrEmpty(RoleName))
            {
                list = list.WhereIf(!string.IsNullOrEmpty(RoleName), x => x.RoleName.Contains(RoleName));
            }

           var roles = await list.OrderBy(x=>x.Id).Skip((pageIndex-1)*pageSize).Take(pageSize).ToListAsync();
            var dto = ObjectMapper.Map<List<RoleTable>, List<RoleDto>>(roles);


            return new PageUserRoleDto<List<RoleDto>> { Code = Result.Success, Data = dto, totalCount = totalcount };

        }
        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="id"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<ResultDto<CreateandUpdateRole>> UpdateRoleAsync(int id,CreateandUpdateRole input)
        {
            var role = await Role.GetAsync(id);
            role.RoleName= input.RoleName;
            role.RoleContent= input.RoleContent;
            var dto = ObjectMapper.Map<RoleTable, CreateandUpdateRole>(role);
            return new ResultDto<CreateandUpdateRole> { Code = Result.Success, Data = dto };
        }
    }
}
